package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.DateUtils;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusSubject;
import com.meida.module.bus.client.entity.BusTestpaper;
import com.meida.module.bus.client.entity.BusTutorType;
import com.meida.module.bus.client.enums.PayTypeEnum;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.module.bus.provider.service.BusSubjectService;
import com.meida.module.bus.provider.utils.MyDateUtils;
import com.meida.module.file.client.entity.SysFile;
import com.meida.module.file.provider.service.SysFileService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 订单详情
 *
 * @author zyf
 */
@Component
public class BusOrderGetHandler implements GetInterceptor {


    @Autowired
    private SysFileService sysFileService;

    @Autowired
    private HttpServletRequest httpServletRequest;


    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.clear();
        cq.select(BusOrder.class, "orderId", "orderNo", "orderType", "orderAmount", "discountAmount", "rewardAmount", "payableAmount", "commission", "expectedAmount", "orderState", "answerUserId");
        cq.select(BusOrder.class, "subjectId", "subjectIds", "orderDescribe", "appointmentDate", "appointmentTime", "durationTime", "expiryTime", "payableAmount", "payState", "payType", "payTime", "createTime");
        cq.select(BusSubject.class, "subjectTitle");
        cq.select(BusTutorType.class, "tutorTypeName", "tutorPrice", "priceUnit");
        cq.select(AppUser.class, "nickName", "userNo", "avatar");
        cq.eq(BusOrder.class, "orderId");
        cq.createJoin(BusSubject.class).setMainField("subjectId").setJoinField("subjectId");
        cq.createJoin(BusTutorType.class).setMainField("tutorTypeId").setJoinField("tutorTypeId");
        cq.createJoin(AppUser.class).setMainField("answerUserId").setJoinField("userId");

    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        String payType = map.get("payType");
        PayTypeEnum payTypeEnum = PayTypeEnum.getByValue(payType);
        if (FlymeUtils.isNotEmpty(payTypeEnum)) {
            map.put("payTypeText", payTypeEnum.getLabel());
        }
        Long orderId = map.getLong("orderId");
        List<EntityMap> fileList = sysFileService.selectFileList(orderId, BusOrder.class.getSimpleName(), criteriaQuery -> {
            criteriaQuery.select(SysFile.class, "fileId", "fileName", "fileUrl","fileGroup", "ossPath", "createTime");
        });
        map.put("fileList", fileList);
        String targetTimezone = httpServletRequest.getHeader("targetTimezone");
        String appointmentDate=map.get("appointmentDate");
        if(FlymeUtils.isNotEmpty(appointmentDate)){
            String appointmentTime=map.get("appointmentTime");
            String appointmentDateTimeStr = appointmentDate + " " + appointmentTime + ":00";
            Date date = DateUtils.strToDate(appointmentDateTimeStr, "yyyy-MM-dd HH:mm:ss");
            Date newDate = MyDateUtils.convertTimezone(date, targetTimezone);
            String newAppointmentDate = DateUtils.formatDate(newDate, "yyyy-MM-dd");
            String newAppointmentTime = DateUtils.formatDate(newDate, "HH:mm");
            map.put("appointmentDate", newAppointmentDate);
            map.put("appointmentTime", newAppointmentTime);
        }
    }


}
